package cn.sms.pet.controller;

import cn.sms.basic.exception.BusinessException;
import cn.sms.basic.util.AjaxResult;
import cn.sms.basic.util.LoginContext;
import cn.sms.basic.util.PageList;
import cn.sms.pet.domain.Pet;
import cn.sms.pet.domain.SearchMasterMsg;
import cn.sms.pet.query.PetTypeQuery;
import cn.sms.pet.query.SearchMasterMsgQuery;
import cn.sms.pet.service.ISearchMasterMsgService;
import cn.sms.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLoginInfo(request);
            seachMasterMsgService.publish(searchMasterMsg,logininfo);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        return seachMasterMsgService.pending(query,LoginContext.getLoginInfo(request));
    }
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        return seachMasterMsgService.processed(query,LoginContext.getLoginInfo(request));
    }
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        return seachMasterMsgService.user(query,LoginContext.getLoginInfo(request));
    }

    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            seachMasterMsgService.handle(pet,loginInfo);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.queryAll();
    }

}
